package com.orange.controller.school;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.controller.BaseController;
import com.orange.handler.AuthItemMapService;
import com.orange.handler.AuthSchoolItemMapService;
import com.orange.handler.AuthSchoolService;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.AuthItemMap;
import com.orange.modules.entity.AuthSchoolItemMap;
import com.orange.modules.entity.school.AuthSchool;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/schoolItem")
public class AuthSchoolItemMapController extends BaseController{
	
	private static final Logger log = Logger.getLogger(AuthSchoolItemMapController.class);
	
	@Autowired
	private AuthSchoolItemMapService authSchoolItemMapService;
	
	@Autowired
	private AuthSchoolService authSchoolService;
	
	@Autowired
	private AuthItemMapService authItemMapService;
	
	/**
	 * 填报认证申请表
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/insertSchoolItem" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertSchoolItem(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try{
			String pid=(String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==pid){
				return toResponse(null,ReturnCode.NOLOGIN,"未登录");
			}
			String subject=(String) request.getSession().getAttribute(SessionUser.SESSION_STATUS);
			
			AuthSchoolItemMap asim = new AuthSchoolItemMap();
			String itemId = map.get("itemId");//分项id
			String reason = map.get("reason");//主要问题
			String selfinstr = map.get("selfinstr");//自评概述
			String singlelevel = map.get("singlelevel");//自评等级
			
			if(StringUtils.isBlank(itemId))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"分项Id不能为空");
			if(StringUtils.isBlank(singlelevel))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"单项等级不能为空");
			else{
				if(!"A".equals(singlelevel) && (StringUtils.isBlank(reason)))//单项等级为B或C的需要填写主要原因
					return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"该等级下需要填写主要问题!");
			}
			if(StringUtils.isBlank(selfinstr))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"自评概述不能为空!");
			asim.setCreatetime(sdf.format(new Date()));
			asim.setItemid(itemId);
			asim.setSinglelevel(singlelevel);
			asim.setLogintype(subject);
			asim.setReason(reason);
			asim.setSelfinstr(selfinstr);
			asim.setSid(pid);
			authSchoolItemMapService.deleteSelfEvaluate(itemId,pid, subject);
			authSchoolItemMapService.insertSelfEvaluate(asim);
			/*AuthSchool as = authSchoolService.queryAuthSchoolInfoBypid(pid);
			authSchoolService.updateAuthSchoolInfo(as);*/
			System.out.println("111111");
			
			AuthItemMap au = this.authItemMapService.queryOneByCondition(pid, itemId);
			if(au == null )
				au = new AuthItemMap();
				au.setS_iswrite("1");
				au.setS_item_id(itemId);
				au.setS_school_id(pid);
				this.authItemMapService.saveAuthItemMap(au);
			
			return toResponse(asim,ReturnCode.SUCCESS,"填报成功");
		}catch(Exception e){
			log.error(e);
			return toResponse(null,ReturnCode.SUCCESS,"填报失败");
		}
		
	}
}
